import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// 动态路由预备
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permissions'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/settings'
import socialRouter from './modules/social_securitys'
import roles from './modules/roles'

Vue.use(VueRouter)
// 动态路由表，项目中不同的用户可以访问不同的功能
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  roles
  // permissionRouter,
  // approvalsRouter,
  // attendancesRouter,
  // salarysRouter,
  // socialRouter
]
// 静态路由表
const staticRoutes = [
  {
    name: "login",
    path: '/login',
    component: () => import(/* webpackChunkName: "login_home" */ '../views/login/login.vue'),
    hidden: true,  // 不渲染到 左侧导航栏
  },
  {
    path: '/',
    redirect: '/dashboard',
    component: () => import(/* webpackChunkName: "login_home" */ '../views/home/home.vue'),
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/dashboard.vue'),
        meta: { title: '首页', icon: 'dashboard' }
      }, {
        path: 'uploadExcel',
        name: 'UploadExcel',
        component: () => import("@/components/UploadExcel"),
        meta: { title: "上传Excel" }
      }, {
        path: 'userInfo',
        name: 'userInfo',
        component: () => import("@/views/user/userinfo.vue"),
        meta: { title: "个人信息" }
      }

    ]
  },
]

const router = new VueRouter({
  routes: staticRoutes
})
// 退出的时候重置路由
export function resetRouter() {
  const newRouter = new VueRouter({
    routes: staticRoutes
  })
  console.log(newRouter);
  router.matcher = newRouter.matcher // 重新设置路由的可匹配路径
}

// 解决报错 Navigation cancelled from "/" to "/login" with a new navigation"报错处理
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}


export default router
